package com.jms.fu.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bo.AppBO;

public class DeleteAppAction extends Action{
	private static Logger logger = Logger.getLogger(LoginAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		PrintWriter out;
		try {
			out = response.getWriter();
		} catch (IOException e1) {
			logger.error(e1.getMessage());
			return null;
		}
		
		try {
			AppBO appbo = new AppBO();
			int	appId = Integer.parseInt(request.getParameter("appid"));
			App app = appbo.getAppByID(appId);
			String appName = app.getAppName();
			String appFolder = getServlet().getServletContext().getRealPath("/")+app.getLink();
			File appF = new File(appFolder);
			FileUtils.deleteDirectory(appF);
			appbo.deleteApp(appId);
			out.print("Delete Success "+appName);
			out.close();
			return null;
		} catch (IOException | DAOException |NumberFormatException e) {
			out.print("Delete failed");
			out.close();
			logger.error(e.getMessage());
			return null;
		}
	}
}
